package com.bhs.sansonglogistics.ui.mine;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.bean.CommonProblemBean;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.adapter.CommonProblemAdapter;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 常见问题
 */
public class CommonProblemActivity extends BaseActivity implements NetCallBack {

    private RecyclerView mRvList;
    private CommonProblemAdapter mAdapter;

    @Override
    public int initLayout() {
        return R.layout.activity_common_problem;
    }

    @Override
    public void initView() {
        TextView mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("常见问题");
        mRvList = findViewById(R.id.rv_list);
        mAdapter = new CommonProblemAdapter();
    }

    @Override
    public void initData() {
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter.setEmptyView(LayoutInflater.from(this).inflate(R.layout.layout_empty, null));
        networkRequest(netApi.get_question_list(1, 100, "logistics"), this);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                CommonProblemBean.DataDTO.ListDTO data = mAdapter.getData().get(position);
                startActivity(new Intent(mActivity,CommonProblemDetailActivity.class)
                        .putExtra("title",data.getTitle())
                        .putExtra("id",data.getId()));
            }
        });
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        CommonProblemBean commonProblemBean = new Gson().fromJson(json, CommonProblemBean.class);
        if (commonProblemBean.isStatus()) {
            mAdapter.setNewData(commonProblemBean.getData().getList());
        } else {
            ToastUtil.show(commonProblemBean.getMsg());
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }
}